<template>
  <el-dialog title="升级设备列表" :model-value="visible" width="1000px" append-to-body @close="handleClose">
    <el-table :data="upgradeDevicesList" style="width: 100%" max-height="400">
      <el-table-column label="设备ID" prop="deviceId" width="120" />
      <el-table-column label="设备名称" prop="deviceName" width="180" />
      <el-table-column label="当前版本" prop="currentVersion" width="120" />
      <el-table-column label="目标版本" prop="targetVersion" width="120" />
      <el-table-column label="升级状态" prop="upgradeStatus" width="120">
        <template #default="scope">
          <el-tag :type="getUpgradeStatusTagType(scope.row.upgradeStatus)">
            {{ getUpgradeStatusText(scope.row.upgradeStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="升级进度" prop="progress" width="120">
        <template #default="scope">
          <el-progress :percentage="scope.row.progress" :status="scope.row.upgradeStatus === 'failed' ? 'exception' : undefined" />
        </template>
      </el-table-column>
      <el-table-column label="开始时间" prop="startTime" width="160" />
      <el-table-column label="完成时间" prop="endTime" width="160" />
      <el-table-column label="错误信息" prop="errorMessage" show-overflow-tooltip />
    </el-table>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleRefresh">刷新</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="UpgradeDevicesDialog" lang="ts">
// Props
interface Props {
  visible: boolean
  upgradeDevicesList: any[]
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  upgradeDevicesList: () => []
})

// Emits
const emit = defineEmits(['update:visible', 'refresh'])

// 获取升级状态标签类型
const getUpgradeStatusTagType = (status: string) => {
  const statusMap = {
    pending: 'info',
    upgrading: 'warning',
    success: 'success',
    failed: 'danger'
  }
  return statusMap[status] || 'info'
}

// 获取升级状态文本
const getUpgradeStatusText = (status: string) => {
  const statusMap = {
    pending: '待升级',
    upgrading: '升级中',
    success: '成功',
    failed: '失败'
  }
  return statusMap[status] || status
}

// 关闭对话框
const handleClose = () => {
  emit('update:visible', false)
}

// 刷新数据
const handleRefresh = () => {
  emit('refresh')
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>